package com.woniuxy.enjoylife.controller;


import com.woniuxy.enjoylife.entity.Goods;
import com.woniuxy.enjoylife.entity.Goods_times;
import com.woniuxy.enjoylife.entity.dto.ReturnGoodsDto;
import com.woniuxy.enjoylife.entity.dto.SelectGoodsDto;
import com.woniuxy.enjoylife.service.GoodsService;
import com.woniuxy.enjoylife.utils.PageResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author teamEnjoyLife75
 * @since 2021-01-23
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Resource
    private GoodsService goodsService;
    @Resource
    private HttpServletRequest request;

    //分页查询商品
    @RequestMapping("/selectGoods")
    public PageResult<ReturnGoodsDto> selectGoods(SelectGoodsDto dto) {

        dto.setSelector((int) request.getSession().getAttribute("id"));
        return goodsService.selectGoodsDto(dto);
    }

    //分页查询个人的商品
    @RequestMapping("/selectMyGoods")
    public PageResult<ReturnGoodsDto> selectMyGoods(SelectGoodsDto dto) {
        dto.setSelector((int) request.getSession().getAttribute("id"));
        dto.setUid((int) request.getSession().getAttribute("id"));
        return goodsService.selectGoodsDto(dto);
    }


    //增加浏览次数
    @RequestMapping("/updateViewTimes")
    public Boolean updateViewTimes(int id) {
        int uid = (int) request.getSession().getAttribute("id");
        return goodsService.updateGoodsTimes(id, uid);
    }

    //点赞或取消
    @RequestMapping("/agreeGoods")
    public Boolean agreeGoods(int id) {
        int uid = (int) request.getSession().getAttribute("id");
        return goodsService.agreeGoods(id, uid);
    }


    @RequestMapping("/deleteGoods")
    public Boolean deleteGoods(Goods goods) {
        return goodsService.deleteGoods(goods);
    }

    @RequestMapping("/insertGoods")
    public Boolean insertGoods(@RequestBody Goods goods) {
        goods.setUid((int) request.getSession().getAttribute("id"));
        return goodsService.save(goods);

    }


}

